home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / DRIVERS.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  10KB  |  652 lines

  1. #include <stdio.h>
  2. #include "vogl.h"
  3. #include "vodevice.h"
  4.  
  5. extern char    *getenv();
  6.  
  7. struct vdev    vdevice;
  8.  
  9. static FILE    *fp = stdout;
  10.  
  11. static int    allocated = 0;
  12.  
  13. void        gexit();
  14.  
  15. /* device-independent function routines */
  16.  
  17. /*
  18.  * voutput
  19.  *
  20.  *    redirect output - only for postscript, hpgl (this is not a feature)
  21.  */
  22. void
  23. voutput(path)
  24.     char    *path;
  25. {
  26.     char    buf[128];
  27.  
  28.     if ((fp = fopen(path, "w")) == (FILE *)NULL) {
  29.         sprintf(buf, "voutput: couldn't open %s", path);
  30.         verror(buf);
  31.     }
  32. }
  33.  
  34. /*
  35.  * _voutfile
  36.  *
  37.  *    return a pointer to the current output file - designed for internal
  38.  * use only.
  39.  */
  40. FILE *
  41. _voutfile()
  42. {
  43.     return(fp);
  44. }
  45.  
  46. /*
  47.  * verror
  48.  *
  49.  *    print an error on the graphics device, and then exit. Only called
  50.  * for fatal errors. We assume that stderr is always there.
  51.  *
  52.  */
  53. void
  54. verror(str)
  55.     char    *str;
  56. {
  57.     if (vdevice.initialised)
  58.         gexit();
  59.  
  60.     fprintf(stderr, "%s\n", str);
  61.     exit(1);
  62. }
  63.  
  64. /*
  65.  * gexit
  66.  *
  67.  *    exit the vogl/vogle system
  68.  *
  69.  */
  70. void
  71. gexit()
  72. {
  73.     if (!vdevice.initialised)
  74.         verror("gexit: vogl not initialised");
  75.  
  76.     (*vdevice.dev.Vexit)();
  77.  
  78.     vdevice.devname = (char *)NULL;
  79.     vdevice.initialised = 0;
  80.     fp = stdout;
  81. }
  82.  
  83. /*
  84.  * getdev
  85.  *
  86.  *    get the appropriate device table structure
  87.  */
  88. static void
  89. getdev(device)
  90.     char    *device;
  91. {
  92. #ifdef THINK_C
  93.     if (strncmp(device, "MacII", 5) == 0)
  94.         _MacII_devcpy();
  95.     else
  96. #endif
  97. #ifdef SUN
  98.     if (strncmp(device, "sun", 3) == 0)
  99.         _SUN_devcpy();
  100.     else
  101. #endif
  102. #ifdef X11
  103.     if (strncmp(device, "X11", 3) == 0)
  104.         _X11_devcpy();
  105.     else
  106. #endif
  107. #ifdef DECX11
  108.     if (strncmp(device, "decX11", 6) == 0)
  109.         _DECX11_devcpy();
  110.     else
  111. #endif
  112. #ifdef NeXT
  113.     if (strncmp(device, "NeXT", 4) == 0)
  114.         _NeXT_devcpy();
  115.     else
  116. #endif
  117. #ifdef POSTSCRIPT
  118.     if (strncmp(device, "postscript", 10) == 0) {
  119.         _PS_devcpy();
  120.     } else
  121.     if (strncmp(device, "ppostscript", 11) == 0) {
  122.         _PSP_devcpy();
  123.     } else
  124. #endif
  125. #ifdef HPGL
  126.     if (strncmp(device, "hpgla1", 6) == 0)
  127.         _HPGL_A1_devcpy();
  128.     else if (strncmp(device, "hpgla3", 6) == 0)
  129.         _HPGL_A3_devcpy();
  130.     else if (strncmp(device, "hpgla4", 6) == 0)
  131.         _HPGL_A4_devcpy();
  132.     else if (strncmp(device, "hpgla2", 6) == 0 || strncmp(device, "hpgl", 4) == 0)
  133.         _HPGL_A2_devcpy();
  134.     else
  135. #endif
  136. #ifdef DXY
  137.     if (strncmp(device, "dxy", 3) == 0)
  138.         _DXY_devcpy();
  139.     else
  140. #endif
  141. #ifdef TEK
  142.     if (strncmp(device, "tek", 3) == 0)
  143.         _TEK_devcpy();
  144.     else
  145. #endif
  146. #ifdef HERCULES
  147.     if (strncmp(device, "hercules", 8) == 0)
  148.         _hgc_devcpy();
  149.     else
  150. #endif
  151. #ifdef CGA
  152.     if (strncmp(device, "cga", 3) == 0)
  153.         _cga_devcpy();
  154.     else
  155. #endif
  156. #ifdef EGA
  157.     if (strncmp(device, "ega", 3) == 0)
  158.         _ega_devcpy();
  159.     else
  160. #endif
  161. #ifdef VGA
  162.     if (strncmp(device, "vga", 3) == 0)
  163.         _vga_devcpy();
  164.     else
  165. #endif
  166. #ifdef SIGMA
  167.     if (strncmp(device, "sigma", 5) == 0)
  168.         _sigma_devcpy();
  169.     else
  170. #endif
  171.     {
  172.         if (*device == 0)
  173.             fprintf(stderr, "vogl: expected the enviroment variable VDEVICE to be set to the desired device.\n");
  174.         else
  175.             fprintf(stderr, "vogl: %s is an invalid device type\n", device);
  176.         fprintf(stderr, "The devices compiled into this library are:\n");
  177. #ifdef THINK_C
  178.         fprintf(stderr, "MacII\n");
  179. #endif
  180. #ifdef SUN
  181.         fprintf(stderr, "sun\n");
  182. #endif
  183. #ifdef X11
  184.         fprintf(stderr, "X11\n");
  185. #endif
  186. #ifdef DECX11
  187.         fprintf(stderr, "decX11\n");
  188. #endif
  189. #ifdef NeXT
  190.         fprintf(stderr, "NeXT\n");
  191. #endif
  192. #ifdef POSTSCRIPT
  193.         fprintf(stderr, "postscript\n");
  194.         fprintf(stderr, "ppostscript\n");
  195. #endif
  196. #ifdef HPGL
  197.         fprintf(stderr, "hpgla1\n");
  198.         fprintf(stderr, "hpgla2 (or hpgl)\n");
  199.         fprintf(stderr, "hpgla3\n");
  200.         fprintf(stderr, "hpgla4\n");
  201. #endif
  202. #ifdef DXY
  203.         fprintf(stderr, "dxy\n");
  204. #endif
  205. #ifdef TEK
  206.         fprintf(stderr, "tek\n");
  207. #endif
  208. #ifdef HERCULES
  209.         fprintf(stderr, "hercules\n");
  210. #endif
  211. #ifdef CGA
  212.         fprintf(stderr, "cga\n");
  213. #endif
  214. #ifdef EGA
  215.         fprintf(stderr, "ega\n");
  216. #endif
  217. #ifdef VGA
  218.         fprintf(stderr, "vga\n");
  219. #endif
  220. #ifdef SIGMA
  221.         fprintf(stderr, "sigma\n");
  222. #endif
  223.         exit(1);
  224.     }
  225. }
  226.  
  227. /*
  228.  * vinit
  229.  *
  230.  *     Just set the device name. ginit and winopen are basically
  231.  * the same as the VOGLE the vinit function.
  232.  *
  233.  */
  234. void
  235. vinit(device)
  236.     char    *device;
  237. {
  238.     vdevice.devname = device;
  239. }
  240.  
  241. /*
  242.  * winopen
  243.  *
  244.  *    use the more modern winopen call (this really calls ginit),
  245.  * we use the title if we can
  246.  */
  247. long
  248. winopen(title)
  249.     char    *title;
  250. {
  251.     vdevice.wintitle = title;
  252.  
  253.     ginit();
  254.  
  255.     return(1L);
  256. }
  257.  
  258. /*
  259.  * ginit
  260.  *
  261.  *    by default we check the environment variable, if nothing
  262.  * is set we use the value passed to us by the vinit call.
  263.  */
  264. void
  265. ginit()
  266. {
  267.     char    *dev;
  268.     int    i;
  269.  
  270. #ifdef THINK_C
  271.     if (vdevice.devname == (char *)NULL) {
  272.         dev = "MacII";
  273.         if (dev == (char *)NULL)
  274.             getdev("");
  275.         else
  276.             getdev(dev);
  277.     } else 
  278.         getdev(vdevice.devname);
  279. #else
  280.     if (vdevice.devname == (char *)NULL) {
  281.         if ((dev = getenv("VDEVICE")) == (char *)NULL)
  282.             getdev("");
  283.         else
  284.             getdev(dev);
  285.     } else 
  286.         getdev(vdevice.devname);
  287. #endif
  288.  
  289.     if (vdevice.initialised)
  290.         gexit();
  291.  
  292.     if (!allocated) {
  293.         allocated = 1;
  294.         vdevice.transmat = (Mstack *)vallocate(sizeof(Mstack));
  295.         vdevice.transmat->back = (Mstack *)NULL;
  296.         vdevice.attr = (Astack *)vallocate(sizeof(Astack));
  297.         vdevice.attr->back = (Astack *)NULL;
  298.         vdevice.viewport = (Vstack *)vallocate(sizeof(Vstack));
  299.         vdevice.viewport->back = (Vstack *)NULL;
  300.         vdevice.bases = (Matrix *)vallocate(sizeof(Matrix) * 10);
  301.         vdevice.enabled = (char *)vallocate(MAXDEVTABSIZE);
  302.     }
  303.  
  304.     for (i = 0; i < MAXDEVTABSIZE; i++)
  305.         vdevice.enabled[i] = 0;
  306.  
  307.     vdevice.alreadyread = vdevice.data = vdevice.devno = 0;
  308.     vdevice.kbdmode = vdevice.mouseevents = vdevice.kbdevents = 0;
  309.  
  310.     vdevice.clipoff = 0;
  311.     vdevice.cpW[V_W] = 1.0;            /* never changes */
  312.  
  313.     vdevice.maxfontnum = 2;
  314.  
  315.     vdevice.attr->a.fontnum = 0;
  316.     vdevice.attr->a.mode = 0;
  317.     vdevice.attr->a.backface = 0;
  318.  
  319.     if ((*vdevice.dev.Vinit)()) {
  320.         vdevice.initialised = 1;
  321.  
  322.         viewport((Screencoord)0, (Screencoord)vdevice.sizeSx,
  323.             (Screencoord)0, (Screencoord)vdevice.sizeSy);
  324.  
  325.         ortho2(0.0, (Coord)vdevice.sizeSx, 0.0, (Coord)vdevice.sizeSy);
  326.  
  327. /*
  328.         identmatrix(vdevice.transmat->m);
  329.         _mapmsave(vdevice.transmat->m);
  330. */
  331.  
  332.         move(0.0, 0.0, 0.0);
  333.  
  334.         font(0);    /* set up default font */
  335.  
  336.         vdevice.inobject = 0;
  337.         vdevice.inpolygon = 0;
  338.     } else {
  339.         fprintf(stderr, "vogl: error while setting up device\n");
  340.         exit(1);
  341.     }
  342.  
  343.     vdevice.alreadyread = 0;
  344.     vdevice.mouseevents = 0;
  345.     vdevice.kbdevents = 0;
  346.     vdevice.kbdmode = 0;
  347.  
  348.     vdevice.concave = 0;
  349. }
  350.  
  351. /*
  352.  * gconfig
  353.  *
  354.  *    thankfully a noop.
  355.  */
  356. void
  357. gconfig()
  358. {
  359. }
  360.  
  361. /*
  362.  * vnewdev
  363.  *
  364.  * reinitialize vogl to use a new device but don't change any
  365.  * global attributes like the window and viewport settings.
  366.  */
  367. void
  368. vnewdev(device)
  369.     char    *device;
  370. {
  371.     if (!vdevice.initialised)
  372.         verror("vnewdev: vogl not initialised\n");
  373.  
  374.     pushviewport();    
  375.  
  376.     (*vdevice.dev.Vexit)();
  377.  
  378.     vdevice.initialised = 0;
  379.  
  380.     getdev(device);
  381.  
  382.     (*vdevice.dev.Vinit)();
  383.  
  384.     vdevice.initialised = 1;
  385.  
  386.     /*
  387.      * Need to update font for this device...
  388.      */
  389.     font(vdevice.attr->a.fontnum);
  390.  
  391.  
  392.     popviewport();
  393. }
  394.  
  395. /*
  396.  * vgetdev
  397.  *
  398.  *    Returns the name of the current vogl device 
  399.  *    in the buffer buf. Also returns a pointer to
  400.  *    the start of buf.
  401.  */
  402. char    *
  403. vgetdev(buf)
  404.     char    *buf;
  405. {
  406.     /*
  407.      * Note no exit if not initialized here - so that gexit
  408.      * can be called before printing the name.
  409.      */
  410.     if (vdevice.dev.devname)
  411.         strcpy(buf, vdevice.dev.devname);
  412.     else
  413.         strcpy(buf, "(no device)");
  414.  
  415.     return(&buf[0]);
  416. }
  417.  
  418. /*
  419.  * getvaluator
  420.  *
  421.  *    similar to the VOGLE locator only it returns either x (MOUSEX) or y (MOUSEY).
  422.  */
  423. long
  424. getvaluator(dev)
  425.     Device    dev;
  426. {
  427.     int    a, b, c;
  428.  
  429.     if (!vdevice.initialised)
  430.         verror("getvaluator: vogl not initialised");
  431.  
  432.     c = (*vdevice.dev.Vlocator)(&a, &b);
  433.  
  434.     if (c != -1) {
  435.         if (dev == MOUSEX)
  436.             return((long)a);
  437.         else 
  438.             return((long)b);
  439.     }
  440.  
  441.     return(-1);
  442. }
  443.  
  444. /*
  445.  * getbutton
  446.  *
  447.  *    returns the up (or down) state of a button. 1 means down, 0 up,
  448.  * -1 invalid.
  449.  */
  450. Boolean
  451. getbutton(dev)
  452.     Device    dev;
  453. {
  454.     int    a, b, c;
  455.  
  456.     if (dev < 256) {
  457.         c = (*vdevice.dev.Vcheckkey)();
  458.         if (c >= 'a' && c <= 'z')
  459.             c = c - 'a' + 'A';
  460.         if (c == dev)
  461.             return(1);
  462.         return(0);
  463.     } else if (dev < 261) {
  464.         c = (*vdevice.dev.Vlocator)(&a, &b);
  465.         if (c & 0x01 && dev == MOUSE3)
  466.             return(1);
  467.         if (c & 0x02 && dev == MOUSE2)
  468.             return(1);
  469.         if (c & 0x04 && dev == MOUSE1)
  470.             return(1);
  471.         return(0);
  472.     }
  473.  
  474.     return(-1);
  475. }
  476.  
  477. /*
  478.  * clear
  479.  *
  480.  *    clears the screen to the current colour, excepting devices
  481.  * like a laser printer where it flushes the page.
  482.  *
  483.  */
  484. void
  485. clear()
  486. {
  487.     Token    *tok;
  488.  
  489.     if (!vdevice.initialised)
  490.         verror("clear: vogl not initialised");
  491.  
  492.     if (vdevice.inobject) {
  493.         tok = newtokens(1);
  494.         tok->i = CLEAR;
  495.  
  496.         return;
  497.     }
  498.  
  499.     (*vdevice.dev.Vclear)();
  500. }
  501.  
  502. /*
  503.  * colorf
  504.  *
  505.  *    set the current colour to colour index given by
  506.  * the rounded value of f.
  507.  *
  508.  */
  509. void
  510. colorf(f)
  511.     double    f;
  512. {
  513.     color((int)(f + 0.5));
  514. }
  515.  
  516. /*
  517.  * color
  518.  *
  519.  *    set the current colour to colour index number i.
  520.  *
  521.  */
  522. void
  523. color(i)
  524.     int    i;
  525. {
  526.     Token    *tok;
  527.  
  528.     if (!vdevice.initialised)
  529.         verror("color: vogl not initialised");
  530.  
  531.     if (vdevice.inobject) {
  532.         tok = newtokens(2);
  533.  
  534.         tok[0].i = COLOR;
  535.         tok[1].i = i;
  536.         return;
  537.     }
  538.  
  539.     vdevice.attr->a.color = i;
  540.     (*vdevice.dev.Vcolor)(i);
  541. }
  542.  
  543. /*
  544.  * mapcolor
  545.  *
  546.  *    set the color of index i.
  547.  */
  548. void
  549. mapcolor(i, r, g, b)
  550.     Colorindex    i;
  551.     short        r, g, b;
  552. {
  553.     Token    *tok;
  554.  
  555.     if (!vdevice.initialised)
  556.         verror("mapcolor: vogl not initialised");
  557.  
  558.     if (vdevice.inobject) {
  559.         tok = newtokens(5);
  560.  
  561.         tok[0].i = MAPCOLOR;
  562.         tok[1].i = i;
  563.         tok[2].i = r;
  564.         tok[3].i = g;
  565.         tok[4].i = b;
  566.  
  567.         return;
  568.     }
  569.  
  570.     (*vdevice.dev.Vmapcolor)(i, r, g, b);
  571. }
  572.  
  573. /*
  574.  * getplanes
  575.  *
  576.  *    Returns the number if bit planes on a device.
  577.  */
  578. long
  579. getplanes()
  580. {
  581.     if (!vdevice.initialised)
  582.         verror("getdepth: vogl not initialised\n");
  583.  
  584.     return((long)vdevice.depth);
  585. }
  586.  
  587. /*
  588.  * reshapeviewport
  589.  *        - does nothing
  590.  */
  591. void
  592. reshapeviewport()
  593. {
  594. }
  595.  
  596. /*
  597.  * winconstraints
  598.  *        - does nothing
  599.  */
  600. void
  601. winconstraints()
  602. {
  603. }
  604.  
  605. /*
  606.  * keepaspect
  607.  *        - does nothing
  608.  */
  609. void
  610. keepaspect()
  611. {
  612. }
  613.  
  614. /*
  615.  * shademodel
  616.  *        - does nothing
  617.  */
  618. void
  619. shademodel(model)
  620.     long    model;
  621. {
  622. }
  623.  
  624. /*
  625.  * getgdesc
  626.  *
  627.  *    Inquire about some stuff....
  628.  */
  629. long
  630. getgdesc(inq)
  631.     long    inq;
  632. {    
  633.     /*
  634.      * How can we know before the device is inited??
  635.      */
  636.  
  637.     switch (inq) {
  638.     case GD_XPMAX:
  639.         if (vdevice.initialised)
  640.             return((long)vdevice.sizeSx);
  641.         else
  642.             return(500L);    /* A bullshit number */
  643.     case GD_YPMAX:
  644.         if (vdevice.initialised)
  645.             return((long)vdevice.sizeSy);
  646.         else
  647.             return(500L);
  648.     default:
  649.         return(-1L);
  650.     }
  651. }
  652.